﻿using Callisto.Controls;
using StudentLive.DataModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234233

namespace StudentLive.InnerPages
{
    /// <summary>
    /// A page that displays a collection of item previews.  In the Split Application this page
    /// is used to display and select one of the available groups.
    /// </summary>
    public sealed partial class StudentsPage : StudentLive.Common.LayoutAwarePage
    {
        public StudentsPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            //RaiseEvent(new RoutedEventArgs(Button.ClickEvent, myButton));
            this.DefaultViewModel["Items"] = LiveDataSource.GetBudies();
        }

        private async void SendEmailButton_Click(object sender, RoutedEventArgs e)
        {
            var student = (StudentItem)itemGridView.SelectedItem;
            string address = student.Email;
            string body = string.Format("\n\n\n\n\nSent By LearN’Go.");
            var mailto = new Uri(string.Format("mailto:?to={0}&body={1}", address, body));
            await Windows.System.Launcher.LaunchUriAsync(mailto);
        }

        private void ItemListView_ItemClick(object sender, ItemClickEventArgs e)
        {

            StudentCard std = new StudentCard();
            std.DataContext = e.ClickedItem;
            //TODO: Commented by Eduardo
            //Flyout f = new Flyout();
            //f.Placement = PlacementMode.Bottom;
            //f.PlacementTarget = sender as UIElement; // this is an UI element (usually the sender)
            //f.Content = std;
            //f.Height = 300; f.Width = 600;
            //f.VerticalOffset = Window.Current.Bounds.Height / 2 - f.Height / 2;

            //f.Background = new SolidColorBrush(Windows.UI.Colors.White);

            //f.IsOpen = true;

        }

        private void itemGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(itemGridView.SelectedItem != null)
            {
                BottomAppBar.Visibility = Visibility.Visible;
                BottomAppBar.IsOpen=true;
                BottomAppBar.IsSticky = true;
            }
            else
            {
                BottomAppBar.Visibility = Visibility.Collapsed;
            }
        }
    }
}
